package app.database;

import app.database.impl.IRoundData;
import app.database.impl.ISTMatch;
import app.database.impl.ITracks;
import app.env.Data;
import app.env.Position;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Name: crubUtils
 * @Description:
 * @Author: colfish
 * @Date: 2021/5/22 16:21
 * 好好学习，天天向上！
 */
public class CrubUtils {
    private InputStream in;
    private SqlSessionFactory factory;
    private SqlSession session;

    private void init(){
        //1.读取配置文件
        InputStream in = null;
        try {
            in = Resources.getResourceAsStream("SqlMapConfig.xml");
            //2.创建SqlSessionFactory工厂
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            factory = builder.build(in);
            //3.使用工厂生产SqlSession对象
            session = factory.openSession();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void saveTracks(String agentName, ArrayList<Position> tracksPart){
        try{
            init();

            //4.使用SqlSession创建Dao接口的代理对象
            ITracks tracksDao = session.getMapper(ITracks.class);
            Tracks track = new Tracks();
            track.setRound(Data.ROUND);
            track.setAgentName(agentName);
            track.setSaveTime(new Date());
            Integer step  = tracksDao.getMaxStep(agentName, Data.ROUND);
            if(step==null)
                step = 0;
            else
                step += 1;
            for (Position position : tracksPart) {
                track.setStep(step);
                track.setCordX(position.x);
                track.setCordY(position.y);
                tracksDao.saveTrack(track);
                step++;
            }

            destory();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public List<Position> getTrackByName(String agentName){
        init();

        //4.使用SqlSession创建Dao接口的代理对象
        ITracks tracksDao = session.getMapper(ITracks.class);
        List<Tracks> tracks = tracksDao.selectByName(agentName, Data.ROUND);

        List<Position> retTrack = new ArrayList<>();

        for(Tracks track: tracks){
            Position position = new Position(track.getCordX(), track.getCordY());

            retTrack.add(position);
        }

        destory();

        return retTrack;
    }


    public Integer getMaxRound(){
        init();

        //4.使用SqlSession创建Dao接口的代理对象
        ITracks tracksDao = session.getMapper(ITracks.class);
        Integer round = tracksDao.getRoundNum()==null ? 0 : tracksDao.getRoundNum();

        destory();

        return round;
    }

    public void saveMatchs(String targetName, String searchName){
        init();

        ISTMatch istMatchDao = session.getMapper(ISTMatch.class);

        STMatch stMatch = new STMatch();

        stMatch.setRound(Data.ROUND);
        stMatch.setSaveTime(new Date());
        stMatch.setSearchName(searchName);
        stMatch.setTargetName(targetName);

        istMatchDao.saveData(stMatch);

        destory();
    }

    public List<String> getMatchSearch(String targetName){
        init();

        ISTMatch istMatchDao = session.getMapper(ISTMatch.class);

        List<String> searchNames = istMatchDao.getSearchName(Data.ROUND, targetName);

        destory();

        return searchNames;
    }




    public RoundData getRoundData(Integer round){
        init();

        IRoundData iRoundDataDao = session.getMapper(IRoundData.class);

        RoundData roundData = iRoundDataDao.getRoundData(round);

        destory();

        return roundData;
    }


    public void saveRoundData(Integer targetNum, Integer searchNum){
        init();

        IRoundData iRoundData = session.getMapper(IRoundData.class);

        RoundData roundData = new RoundData();

        roundData.setRound(Data.ROUND);
        roundData.setSaveTime(new Date());
        roundData.setSearchNum(searchNum);
        roundData.setTargetNum(targetNum);

        iRoundData.saveData(roundData);

        destory();
    }



    private void destory(){
        try {
            session.commit();
            session.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
